package com.changli03.threadsafe.nosafe;

public class noSafeTest {
    public static void main(String[] args) {
        TicketThread tt1 = new TicketThread();
        //共用一个类
        Thread t1 = new Thread(tt1);
        Thread t2 = new Thread(tt1);
        Thread t3 = new Thread(tt1);

        t1.setName("窗口1");
        t2.setName("窗口2");
        t3.setName("窗口3");

        t1.start();
        t2.start();
        t3.start();

    }
}
class TicketThread implements Runnable {
    int ticketnum = 100 ;
    Object lock = new Object();
    @Override
    public void run() {
        while (true){

            if (ticketnum > 0){
                try {
                    Thread.sleep(10);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                System.out.println(Thread.currentThread().getName() + "票号为:" + ticketnum);
                 ticketnum--;
            }else {
                break;
            }

        }
    }
}
